package com.itheima.health.utils;

import com.itheima.health.constant.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;

@Component
public class ScheduleTasks {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AliOSSUtils ossUtils;

    // 使用cron表达式
    @Scheduled(cron = "0 0 12 * * ?")
    public void scheduleTaskUsingCronExpression() {
        System.out.println("定时任务执行，当前时间：" + new Date());

        SetOperations setOperations = redisTemplate.opsForSet();
        //求两个集合的差集
        //注释
        Set<String> difference  = setOperations.difference(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        System.out.println(difference);
        if(difference != null && difference.size() > 0){
            for (String pic : difference) {
                //删除图片
                ossUtils.delete(pic);
                //从Redis集合中删除图片
                setOperations.remove(RedisConstant.SETMEAL_PIC_RESOURCES,pic);
            }
        }
    }

//    // 固定延迟
//    @Scheduled(fixedDelay = 5000)
//    public void scheduleTaskWithFixedDelay() {
//        System.out.println("固定延迟5秒执行的任务，当前时间：" + new Date());
//    }
//
//    // 固定速率
//    @Scheduled(fixedRate = 5000)
//    public void scheduleTaskWithFixedRate() {
//        System.out.println("固定速率5秒执行的任务，当前时间：" + new Date());
//    }
}
